package com.game.manager
{
	import flash.display.NativeWindow;
	
	public class SubWindowManager
	{
		
		private static var _instance:SubWindowManager;
		
		public static function getIns():SubWindowManager
		{
			if (!_instance)
			{
				_instance = new SubWindowManager();
			}
			return _instance;
		}
		
		private var subWindowVec:Vector.<NativeWindow> = new Vector.<NativeWindow>();
		
		public function registerWindow(tarWin:NativeWindow, limitOnly:Boolean = true):void
		{
			if (limitOnly)
			{
				for each (var win:NativeWindow in this.subWindowVec)
				{
					if (win != null && win.title == tarWin.title)
					{
						win.close();
					}
				}
			}
			this.subWindowVec.push(tarWin);
		}
		
		public function hasRegisterWindow(winTitle:String):Boolean
		{
			for each (var win:NativeWindow in this.subWindowVec)
			{
				if (win != null && win.title == winTitle)
				{
					return true;
				}
			}
			return false;
		}
		
		public function getWindowByTitle(winTitle:String):NativeWindow
		{
			for each (var win:NativeWindow in this.subWindowVec)
			{
				if (win != null && win.title == winTitle)
				{
					return win;
				}
			}
			return null;
		}
		
		public function removeRegisteredWindow(tarWin:NativeWindow):void
		{
			var idx:int = this.subWindowVec.indexOf(tarWin);
			if (idx != -1)
			{
				this.subWindowVec.splice(idx, 1);
			}
		}
		
		public function closeAllWindow(... rest):void
		{
			for each (var win:NativeWindow in this.subWindowVec)
			{
				if (win != null && !win.closed)
				{
					win.close();
				}
			}
		}
	}
}
